const Router = require('@koa/router')
const jwt = require("jsonwebtoken")
const { UserModel } = require('../databases/index')
const router = new Router()
router.get("/getAllUser", async(ctx) => {
    const user = await UserModel.findAll();
    ctx.body = {
        code: 200,
        msg: "success",
        data: {
            user
        }
    }
})
router.get("/getUser", async(ctx) => {
    const { currentPage, pageSize } = ctx.request.query;
    // console.log( currentPage,pageSize)
    offset = (currentPage - 1) * pageSize || 0;
    const result = await UserModel.findAndCountAll({
        offset,
        limit: parseInt(pageSize),
    });
    if (result) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                result
            }
        }
    }

})
router.post("/login", async(ctx) => {
    const { username, password } = ctx.request.body;
    console.log("用户登录",username, password);
    const user = await UserModel.findOne({ where: { username:username, password:password} })
    console.log(user);
    if (user) {
        const accessToken =
            "Bearer" +
            jwt.sign({
                    userId: "1",
                    username: username,
                },
                "fq", {
                    expiresIn: 3600 * 24 * 7,
                }
            );
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "login success",
                accessToken,
                user,
            }
        }
        console.log("444", accessToken)
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "该用户不存在"
            }
        }
    }
})
router.post("/adminLogin", async(ctx) => {
    const { username, password } = ctx.request.body;
    console.log(username, password);
    const user = await UserModel.findOne({ where: { username, password } })
    // console.log(5656, user.userID);
    if (user.userID) {
        const accessToken =
            "Bearer" +
            jwt.sign({
                    userId: "1",
                    username: username,
                },
                "fq", {
                    expiresIn: 3600 * 24 * 7,
                }
            )
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "login success",
                accessToken,
                user,
            }
        }
        console.log(444, accessToken)
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "该用户不存在"
            }
        }
    }
})
router.post("/register", async(ctx) => {
    const { username, password, tel } = ctx.request.body;
    // console.log(username, password, tel);
    const dl = await UserModel.findOne({ where: { username } })
    if (dl) {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "用户已存在"
            }
        }
    } else {
        const jane = await UserModel.create({ username: username, password: password, tel: tel, usertype: 2 })
        console.log(1212, jane);
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: jane
            }
        }
    }
})
router.post("/search", async(ctx) => {
    const { Op } = require("sequelize");
    const { value } = ctx.request.body;
    console.log("得到的搜索值", value);
    const datas = await UserModel.findAll({
        where: {
            [Op.or]: [{
                    username: {
                        [Op.like]: '%' + value + '%'
                    }
                },
                {
                    usertype: {
                        [Op.like]: '%' + value + '%'
                    }
                }
            ]
        }
    })
    if (datas) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                datas
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})
router.post("/deleteUserById", async(ctx) => {
    const userID = ctx.request.body;
    console.log("选择的用户ID", userID.userID);
    const userid = await UserModel.destroy({ where: { userID: userID.userID } })
    if (userid) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "usersid success",
                userid
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "用户不存在"
            }
        }
    }
})
router.post("/deleteUser", async(ctx) => {
    const { selectUser } = ctx.request.body;
    console.log(selectUser);
    // const select = await UserModel.findAll();
    const select = await UserModel.destroy({ where: { userID: selectUser } });
    console.log('详情', select)
    if (select) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",

            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "失败"
            }
        }
    }
})
router.post("/addUser", async(ctx) => {
    const { addForm } = ctx.request.body;
    console.log(addForm.username);
    const dl = await UserModel.create({ 
        username: addForm.username, password: addForm.password, usertype: addForm.usertype, tel: addForm.tel })
    console.log(dl,11111)
    if (dl) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})
router.post("/getOneUser", async(ctx) => {
    const { userID } = ctx.request.body;
    console.log("获取的ID", userID);
    // const user = await UserModel.findAll();
    const user = await UserModel.findOne({ where: { userID: userID } });
    console.log('用户详情', user)
    if (user) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "getOneUser success",
                user
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "用户不存在"
            }
        }
    }
})
router.post("/updateUser", async(ctx) => {
    let viewsData = ctx.request.body;
    console.log("得到信息", viewsData);
    const user = await UserModel.update({
        username: viewsData.username, //更新userName
        password: viewsData.password, //更新密码
        tel: viewsData.tel, //更新电话
        usertype: viewsData.usertype, //更新类别
    }, {
        where: {
            userID: viewsData.userID
        }
    });
    if (user.length > 0) {
        ctx.body = {
            code: 200,
            msg: "success",
        }
    } else {
        ctx.body = {
            code: 500,
            msg: "fail",
        }
    }
})

module.exports = router